package com.cheegu.common.parser.gpsdata.packet;

import com.cheegu.common.parser.gpsdata.Unpacker;
import com.cheegu.common.parser.gpsdata.packet.boshijie.BoShiJieCmdType;
import com.cheegu.common.parser.gpsdata.unpacker.DefaultUnpacker;
import com.cheegu.common.parser.gpsdata.util.CodingUtils;
/**
 * 博实结包结构
 *
 * @author: zhangxh
 * @email: zhangxh@cheegu.com
 * @date: 2018/3/22 11:50
 */
public class BSJA5CPacket {
    /**
     * 包头
     */
    private byte[] head = {0x29, 0x29};
    /**
     * 指令类型
     */
    public BoShiJieCmdType cmdType = BoShiJieCmdType.Unknown;
    /**
     * 包长
     */
    public short length = 0;
    /**
     * 伪IP
     */
    public String pseudoIp = "";
    /**
     * 终端设备号
     */
    public String deviceNumber = "";
    /**
     * 数据体
     */

    public byte[] dataBuffer = null;

    /**
     * 校验码
     */
    public byte checkCode = 0;

    /**
     * 包尾
     */
    public byte ail = 0x0D;

    public int ReadBuffer(byte[] dataBufferr) throws Exception {
        Unpacker unpacker = new DefaultUnpacker(dataBufferr);
        this.head = unpacker.readBytes(2);
        this.cmdType=BoShiJieCmdType.valueOf(unpacker.readByte());
        this.length = unpacker.readShort();
        byte[] arrPseudoIP = unpacker.readBytes(4);
        this.pseudoIp =String.valueOf(arrPseudoIP[0])+ "." + String.valueOf(arrPseudoIP[1]) + "." + String.valueOf(arrPseudoIP[2]) + "." +String.valueOf(arrPseudoIP[3]);
        this.deviceNumber=CodingUtils.pseudoIPToCode(arrPseudoIP);
        return 0;
    }
}
